package com.woniuxy.order.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.GetOrdersByStallIdAndTimeForm;
import com.woniuxy.core.model.PageInfo;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.RedisUtil;
import com.woniuxy.order.dao.OrderInfo;
import com.woniuxy.order.service.OrderInfoService;
import com.woniuxy.order.service.impl.dto.OrderInfoDTO;
import com.woniuxy.order.service.RenterWaitingToUseOrder;
import com.woniuxy.order.service.param.*;
import com.woniuxy.order.web.controller.form.*;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.TokenRequestEndpoint;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@RequestMapping("/orderInfo")
@Api(tags = "订单详情接口")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private RedisUtil redisUtil;
    @ApiOperation("创建车位使用订单")
    @PostMapping("/creatOrder")
    public Result creatOrder(@RequestBody CreatOrderForm form, @RequestHeader String token) throws Exception {
        Claims claims = JwtUtil.parseUserToken(token);
        Integer id =(Integer) claims.get("id");
        CreatOrderParam param = BeanUtil.toBean(form, CreatOrderParam.class);
        param.setUserInfoId(id);
        orderInfoService.creatOrder(param);
        return Result.getResult().setCode(200).setMessage("订单创建成功");
    }
    @ApiOperation("租客查询待使用订单")
    @PostMapping("/findRenterWaitingToUseOrder")
    public Result findRenterWaitingToUseOrder(@RequestHeader String token) throws Exception {
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer renterId =(Integer) claims.get("id");
        List<RenterWaitingToUseOrder> list=orderInfoService.findRenterWaitingToUseOrder(renterId);
        return Result.getListResult(RenterWaitingToUseOrder.class).setCode(200).setMessage("查询我的待使用订单成功").setData(list);
    }

    @ApiOperation("租客删除待使用订单")
    @GetMapping("/delWaitToUseOrder")
    @ApiImplicitParam(name = "orderInfoNum",value = "订单编号")
    public Result delWaitToUseOrder(@RequestHeader String token,String orderInfoNum) throws Exception {
        Claims claims = JwtUtil.parseUserToken(token);
        Integer renterId =(Integer) claims.get("id");
        orderInfoService.delWaitToUseOrder(renterId,orderInfoNum);
        return Result.getResult().setMessage("删除订单成功");
    }
    @GetMapping("/pagePayEarnest")
    @ApiOperation("支付定金")
    @ApiImplicitParam(name = "orderNum",value = "订单编号")
    public Result pagePayEarnest(String orderNum) throws Exception{
        String html=orderInfoService.pagePayEarnest(orderNum);
        return Result.getResult(String.class).setData(html).setMessage("发起支付定金成功");
    }
    @PostMapping("/pagePayEarnestNotify")
    public String pagePayEarnestNotify(HttpServletRequest request) throws Exception{
        System.out.println("aaaaaaaaaaaa");
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        System.out.println(params);
        orderInfoService.pagePayEarnestNotify(params);
        return "SUCCESS";
    }
    @GetMapping("/pagePay")
    @ApiOperation("支付尾款")
    @ApiImplicitParam(name = "orderNum",value = "订单编号")
    public Result pagePay(String orderNum) throws Exception{
        String html=orderInfoService.pagePay(orderNum);
        return Result.getResult(String.class).setData(html).setMessage("发起尾款成功");
    }
    @PostMapping("/pagePayNotify")
    public String pagePayNotify(HttpServletRequest request) throws Exception{
        System.out.println("aaaaaaaaaaaa");
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        System.out.println(params);
        orderInfoService.pagePayNotify(params);
        return "SUCCESS";
    }

    @PostMapping("/platformGetOrderPage")
    @ApiOperation("平台分页查看普通订单")
    public Result<PageInfo> platformGetOrderPage(@RequestBody PlatformGetOrderForm form) throws Exception{
        PlatformGetOrderParam param = BeanUtil.toBean(form, PlatformGetOrderParam.class);
        PageInfo pageInfo=orderInfoService.platformGetOrderPage(param);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("平台分页查看普通订单成功");
    }

    @PostMapping("/platformGetOverTimeOrderPage")
    @ApiOperation("平台分页查看超时订单")
    public Result<PageInfo> platformGetOverTimeOrderPage(@RequestHeader String token,@RequestBody PlatformGetOverTimeOrderForm form) throws Exception{
        PlatformGetOverTimeOrderParam param = BeanUtil.toBean(form, PlatformGetOverTimeOrderParam.class);
        PageInfo pageInfo=orderInfoService.platformGetOverTimeOrderPage(param);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("平台分页查看超时订单成功");
    }

    @PostMapping("/getRenterOrders")
    @ApiOperation("出租人查看自己车位订单")
    public Result<PageInfo> getRenterOrders(@RequestHeader String token,@RequestBody RenterOrderForm form) throws Exception{
        Claims claims = JwtUtil.parseUserToken(token);
        Integer id =(Integer) claims.get("id");
        RenterOrderParam param = BeanUtil.toBean(form, RenterOrderParam.class);
        param.setRenterId(id);
        PageInfo pageInfo=orderInfoService.getRenterOrders(param);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("出租方查看自己车位订单成功");
    }

    @GetMapping("/getCerOrders")
    @ApiOperation("物业查看自己车位订单")
    public Result<PageInfo> getCerOrders(@RequestHeader String token,Integer current,Integer size) throws Exception{
        Claims claims = JwtUtil.parseUserToken(token);
        Integer id =(Integer) claims.get("id");
        PageInfo pageInfo=orderInfoService.getCerOrders(id,current,size);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("物业查看自己车位订单成功");
    }


    @ApiOperation("根据车牌号查询订单")
    @GetMapping("/selectOrderByCarPlate")
    @ApiImplicitParam(name = "plate",value = "车牌号")
    public Result<List<OrderInfoDTO>> selectOrderByCarPlate(String plate){
        List<OrderInfoDTO> orderInfoDTO = orderInfoService.selectOrderByCarPlate(plate);
        return Result.getListResult(OrderInfoDTO.class).setMessage("车牌号查询订单成功").setData(orderInfoDTO);
    }


    @GetMapping("/getUserOrders")
    @ApiOperation("用户分页查看自己订单")
    public Result<PageInfo> getUserOrders(@RequestHeader String token,Integer current,Integer size) throws Exception{
        Claims claims = JwtUtil.parseUserToken(token);
        Integer id =(Integer) claims.get("id");
        PageInfo pageInfo=orderInfoService.getUserOrders(id,current,size);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("用户分页查看自己订单成功");
    }

    @PostMapping("/getUserOrdersByTime")
    @ApiOperation("用户根据时间段查看自己订单")
    public Result<PageInfo> getUserOrdersByTime(@RequestHeader String token, @RequestBody GetUserOrderByTimeForm form) throws Exception{
        Claims claims = JwtUtil.parseUserToken(token);
        Integer id =(Integer) claims.get("id");
        GetUserOrderByTimeParam param = BeanUtil.toBean(form, GetUserOrderByTimeParam.class);
        param.setUserId(id);
        PageInfo pageInfo=orderInfoService.getUserOrdersByTime(param);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("用户根据时间段查看自己订单成功");
    }

    @PostMapping("/getUserOrdersByState")
    @ApiOperation("用户根据订单状态查看自己订单")
    public Result<PageInfo> getUserOrdersByState(@RequestHeader String token, @RequestBody GetUserOrderByStateForm form) throws Exception{
        Claims claims = JwtUtil.parseUserToken(token);
        Integer id =(Integer) claims.get("id");
        GetUserOrderByStateParam param = BeanUtil.toBean(form, GetUserOrderByStateParam.class);
        param.setUserId(id);
        PageInfo pageInfo=orderInfoService.getUserOrdersByState(param);
        return Result.getResult(PageInfo.class).setData(pageInfo).setMessage("用户根据订单状态查看自己订单成功");
    }

    @PostMapping("/getOrderInfosByStallIds")
    @ApiOperation("根据车位ID数组查询订单")
    public Result<List<OrderInfo>> getOrderInfosByStallIds(@RequestBody Set<Integer> stallIds) throws Exception{
        List<OrderInfo> list=orderInfoService.getOrderInfosByStallIds(stallIds);
        return Result.getListResult(OrderInfo.class).setMessage("根据车位ID数组查询订单成功").setData(list);
    }


    @PostMapping("/getOrdersByStallIdAndTime")
    @ApiOperation("根据车位ID和时间段查询订单")
    public Result<List<OrderInfo>> getOrdersByStallIdAndTime(@RequestBody GetOrdersByStallIdAndTimeForm form) throws Exception{
        List<OrderInfo> list=orderInfoService.getOrdersByStallIdAndTime(form);
        return Result.getListResult(OrderInfo.class).setMessage("根据车位ID和时间段查询订单成功").setData(list);
    }

    @PostMapping("/getNoCommentOrder")
    @ApiOperation("用户查看待评价订单")
    public Result<List<OrderInfoDTO>> getNoCommentOrder(@RequestHeader String token) throws Exception{
        String longToken = redisUtil.get(token);
        Claims claims = JwtUtil.parseUserToken(longToken);
        Integer id =(Integer) claims.get("id");
        List<OrderInfoDTO> list=orderInfoService.getNoCommentOrder(id);
        return Result.getListResult(OrderInfoDTO.class).setMessage("用户查看待评价订单成功").setData(list);
    }
    @GetMapping("/getTodayIncomeByCerId")
    @ApiOperation("物业查看今日营收")
    public Result<String> getTodayIncomeByCerId(Integer certifiedInfoId) throws Exception{
        String bigDecimal=orderInfoService.getTodayIncomeByCerId(certifiedInfoId);
        return Result.getResult(String.class).setMessage("物业查看今日营收成功").setData(bigDecimal);
    }

    @GetMapping("/platGetTodayIncome")
    @ApiOperation("平台查看今日营收")
    public Result<String> platGetTodayIncome() throws Exception{
        String bigDecimal=orderInfoService.platGetTodayIncome();
        return Result.getResult(String.class).setMessage("平台查看今日营收成功").setData(bigDecimal);
    }

    @GetMapping("/renterGetTodayInCome")
    @ApiOperation("业主查看今日营收")
    public Result<String> renterGetTodayInCome(Integer renterId) throws Exception{
        String bigDecimal=orderInfoService.renterGetTodayInCome(renterId);
        return Result.getResult(String.class).setMessage("业主查看今日营收成功").setData(bigDecimal);
    }

    @GetMapping("/cerGetMonthInCome")
    @ApiOperation("物业查看本月营业收入")
    public Result<String> cerGetMonthInCome(Integer certifiedInfoId) throws Exception{
        String bigDecimal=orderInfoService.cerGetMonthInCome(certifiedInfoId);
        return Result.getResult(String.class).setMessage("物业查看本月营业收入成功").setData(bigDecimal);
    }

    @GetMapping("/platGetMonthInCome")
    @ApiOperation("平台查看本月营业收入")
    public Result<String> platGetMonthInCome() throws Exception{
        String bigDecimal=orderInfoService.platGetMonthInCome();
        return Result.getResult(String.class).setMessage("平台查看本月营业收入成功").setData(bigDecimal);
    }

    @GetMapping("/renterGetMonthInCome")
    @ApiOperation("业主查看本月营业收入")
    public Result<String> renterGetMonthInCome(Integer renterId) throws Exception{
        String bigDecimal=orderInfoService.renterGetMonthInCome(renterId);
        return Result.getResult(String.class).setMessage("业主查看本月营业收入成功").setData(bigDecimal);
    }
}

